//
//  ListViewController.m
//  primoPrj
//
//  Created by Giuseppe on 15/11/13.
//
//

#import "ListViewController.h"
#import "DetailViewController.h"
#import "AppDelegate.h"


@interface ListViewController ()

@end

@implementation ListViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 520) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        [self.view  addSubview:_tableView];
        
        AppDelegate* AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
        [AppDel initDatabase];
        _itemList = [[NSMutableArray alloc]initWithArray: [AppDel getElementsList]];
    }
    return self;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    AppDelegate* AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    _itemList = [[NSMutableArray alloc]initWithArray: [AppDel getElementsList]];
    return [_itemList count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
     if (cell == nil){
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
     }
         
    Element *element = [[Element alloc] init];
    element = [_itemList objectAtIndex:indexPath.row];
    
    NSLog(@"--------->_itemListCount: %d", _itemList.count);
    NSLog(@"--------->_itemList: %@", [_itemList objectAtIndex:indexPath.row]);
    
         
    NSString *label1 = [NSString stringWithFormat: @"%@ %@", element.name, element.description];
    cell.textLabel.text = label1;
    NSString *label2 = [NSString stringWithFormat: @"%d %@", element.elementID, element.origin];
    cell.detailTextLabel.text = label2;

    //cell.imageView.image = [self loadImage:element.img];
    // new -->
    AppDelegate* AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    cell.imageView.image = [AppDel loadImage:element.img];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}



/*
- (UIImage *)loadImage: (NSString*)filename
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
        

    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename] ];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
    return image;
}
*/


- (void)insertNewElement:(Element *)element {
    
    NSLog(@"Inseririsco elemento = %@",element);
    
    AppDelegate* AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    [AppDel saveElement:element];
    
    [_itemList removeAllObjects];
    _itemList = [[NSMutableArray alloc]initWithArray: [AppDel getElementsList]];
    [_tableView reloadData];
    
    
    //int i=0;
    //for(i=0;i<_itemList.count;i++)
    //    NSLog(@"_itemList(%d): %@ - id: %d",i,[[_itemList objectAtIndex:i] name],[[_itemList objectAtIndex:i] elementID]);
    

    //[_tableView insertRowsAtIndexPaths:_itemList withRowAnimation:UITableViewRowAnimationRight];
    
    /*//[_itemList removeAllObjects];
    _itemList = [[NSMutableArray alloc]initWithArray: [AppDel getElementsList]];
    int i=0;
    for(i=0;i<_itemList.count;i++)
        NSLog(@"_itemList(%d): %@ - id: %d",i,[[_itemList objectAtIndex:i] name],[[_itemList objectAtIndex:i] elementID]);
    [_tableView reloadData];*/

}

- (void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
   
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithElement: [_itemList objectAtIndex:indexPath.row]];
    
    detailViewController.title = [[_itemList objectAtIndex:indexPath.row] name];
    
    [[self navigationController] pushViewController:detailViewController animated:NO];
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // new -->
        AppDelegate * AppDel = (AppDelegate*)[[UIApplication sharedApplication]delegate];
        [AppDel deleteElement:[_itemList objectAtIndex:indexPath.row]];
        [_itemList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        //...
    }
}


@end